import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const Login = _ => import('../views/Login.vue');
const Home = _ => import('../views/Home.vue');
const Welcome = _ => import('../views/Welcome.vue');
const Users = _ => import('../views/user/Users.vue');
const Rights = _ => import('../views/power/Rights.vue');
const Roles = _ => import('../views/power/Roles.vue');
const Cate = _ => import('../views/goods/Cate.vue');
const Params = _ => import('../views/goods/Params.vue');
const GoodsList = _ => import('../views/goods/List.vue');
const Add = _ => import('../views/goods/Add.vue');
const Order = _ => import('../views/order/Order.vue');
const Report = _ => import('../views/report/Report.vue');

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    {
      name: 'home',
      path: '/home',
      component: Home,
      redirect:'welcome',
      children:[
        {
          path:'/welcome',
          name:'welcome',
          component:Welcome
        },
        {
          path:'/users',
          name:'users',
          component:Users
        },
        {
          path:'/rights',
          name:'rights',
          component:Rights
        },
        {
          path:'/roles',
          name:'roles',
          component:Roles
        },
        {
          path:'/categories',
          name:'cate',
          component:Cate
        },
        {
          path:'/params',
          name:'params',
          component:Params
        },
        {
          path:'/goods',
          name:'goods',
          component:GoodsList,
        },
        {
          path:'/goods/add',
          name:'add',
          component:Add,
        },
        {
          path:'/orders',
          name:'order',
          component:Order,
        },
        {
          path:'/reports',
          name:'report',
          component:Report,
        },

      ]
    }
  ]
})


router.beforeEach((to, from, next) => {
  /*
    导航守卫需求：
      1.跳转的目的地是否是login 如果是，则放行
      2.跳转时判断是否携带token，是则放行，否则跳转到login
      3. 1、2都过了则直接放行
  */
  if (to.name === 'login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('login')
  next()
})

export default router